<?php

/**
 * alltosun.com device_widget.php
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-1-16 上午11:46:44 $
 * $Id: $
 */
class machine_widget
{
    /**
     * TODO device 废弃, 嫁接到 device_helper::get_device_list_by_store_id
     * 获取设备绑定信息概览
     */
    public function get_store_device_info_list($store_id, $filter = array()) {
        if (!$store_id) {
            return array();
        }

        $filter['store_id'] = $store_id;
        $filter['status'] = 2;
        $list = array();
        $device_count = array(
            'MX' => 0,
            'ZX' => 0,
            'FX' => 0,
            'AD' => 0,
            'TA' => 0,
            'OS' => 0,
            'CS' => 0,
            );

        $device_num = _model('store_device_relation')->getTotal($filter);

        foreach($device_num as $v1){
            if($v1['type'] == 'MX'){
                $device_count['MX'] ++;
            } elseif($v1['type'] == 'ZX'){
                $device_count['ZX'] ++;
            } elseif($v1['type'] == 'FX'){
                $device_count['FX'] ++;
            } elseif($v1['type'] == 'AD'){
                $device_count['AD'] ++;
            } elseif($v1['type'] == 'TA'){
                $device_count['TA'] ++;
            } elseif($v1['type'] == 'OS'){
                $device_count['OS'] ++;
            } elseif($v1['type'] == 'CS'){
                $device_count['CS'] ++;
            }
        }
		foreach($device_count as $k=>$devt){
	        $list_arr = array(
	            'type' => $type['type'],
	            'device_num' => $devt[$type['type']],
	            'device_num_str' => '已绑定设备：' . $devt[$type['type']] . '台',
	            'allow_bind' => 1
	        );
	        array_push($list, $list_arr);
		}

        return $list;


    }

    /**
     * 获取门店所有类型的设备绑定的列表
     */
    public function get_store_all_device_list($store_id) {
        if (!$store_id) {
            return array();
        }

        $all_device_list = array();
            $filter['store_id'] = $store_id;
            $filter['status'] = 2;
            $device_relation_list = _model('store_device_relation')->getList($filter);
            if (!$device_relation_list) {
                continue;
            }

            $device_list = array();
            foreach ($device_relation_list as $k => $v) {
                $device_info = _uri('device', $v['dev_id']);
                if (!$device_info) {
                    continue;
                }
                $info['id'] = $device_info['id'];
                $info['device_no'] = $device_info['code'];
                $info['res_name'] = $device_info['type'];
                $info['res_name_str'] = device_config::$TYPES[$device_info['type']]['name'];
                array_push($device_list, $info);
            }

            if ($device_list) {
                array_push($all_device_list, $device_list);
            }

        return $all_device_list;
    }

    /**
     * @TODO device 废弃, 嫁接到 device_helper::get_device_by_store_id
     *
     * 获取门店具体某一类型的绑定设备列表
     */
    public function get_store_device_list($store_id, $type) {
        if (!$store_id) {
            return array();
        }

        if (!in_array($type, device_config::$TYPES)) {
            return array();
        }

        $filter['type'] = $type;
        $filter['store_id'] = $store_id;
        $filter['status'] = 2;
        $device_relation_list = _model('store_device_relation')->getList($filter);
        if (!$device_relation_list) {
            return array();
        }

        $device_list = array();
        foreach ($device_relation_list as $k => $v) {
            $device_info = _uri('device', $v['dev_id']);
            $info['id'] = $device_info['id'];
            $info['device_no'] = $device_info['code'];
            array_push($device_list, $info);

        }
        return $device_list;
    }


    /**
     * @TODO device 废弃, 嫁接到 device_helper::bind_store_device
     *
     * 绑定店铺和设备
     * @param $store_id 店铺id
     * @param $device_no 设备号
     * @param $status 绑定状态  0 =>出厂，1=>停用，2=>绑定， 3=>损坏
     */
    public function bind_store_device($store_id, $device_no, $status, $unbind_reason = '', $extra = '') {
        if (!$store_id || !$device_no) {
            return array('flag' => 'error', 'info' => '没有店铺id或设备号');
        }

        $store_info = store_helper::get_store_info($store_id);
        $res_name = machine_helper::get_device_resname($device_no);
        $type = device_helper::get_device_type_by_code($device_no);

        $device_id = machine_helper::get_device_id($device_no,'device');

        if (!$device_id) {
            $device_id = _model('device')->create(array('code' => $device_no, 'status' => $status));
        } elseif (!$device_id) {
            return array('flag' => 'error', 'info' => '设备序列号不存在');
        }

        $device_relation_info = _uri('store_device_relation', array('dev_id' => $device_id, 'type' => $type));
        $store_device_info = _uri('store_device_relation', array('store_id' => $store_id, 'type' => $type));

        // 防止多设备绑定
        //         if ($store_device_info) {
        //             if (2 == $store_device_info['status'] && $store_device_info['res_id'] != $device_id) {
        //                 // 如果已经有绑定信息判断是否是保温箱,需要进一步判断新老是否相同
        //                 if ($res_name == 'machine') {
        //                     $machine_no = machine_helper::get_device_no($device_id, $res_name);
        //                     $is_old = machine_helper::is_old_machine($machine_no);
        //                     if ($is_old == $is_old_machine) {
        //                         return array('flag'=>'error', 'info'=>'此店铺已绑定过此类型设备');
        //                     }
        //                 } else {
        //                     return array('flag'=>'error', 'info'=>'此店铺已绑定过此类型设备');
        //                 }

        //             }
        //         }

        // 增加或更新绑定关系
        if ($device_relation_info) {
            if (isset($device_relation_info['status']) && 2 == $device_relation_info['status'] && 2 == $status) {
                return array('flag' => 'error', 'info' => '此设备已绑定');
            } elseif (1 == $device_relation_info['status'] && 1 == $status) {
                return array('flag' => 'error', 'info' => '此设备已解除绑定');
            }
            _model('store_device_relation')->update($device_relation_info['id'], array(
                'status' => $status,
                'store_id' => $store_id
            ));
        } else {
            adbox_helper::save_store_device_relation($store_id,$device_id,$type, $res_name, $device_id, $status);
        }

        // 增加设备绑定记录
        // 停用,解除绑定
        if (1 == $status) {
            adbox_helper::save_user_device_record($store_id,$device_id,$type, $res_name, $device_id, $status, $unbind_reason, $extra);
        } else {
            adbox_helper::save_user_device_record($store_id,$device_id,$type, $res_name, $device_id, $status);
        }

        // 修改设备码状态
        machine_helper::update_device_status($device_no, 'device', $status);

        return array('flag' => 'success', 'info' => 'success');

    }

}


?>